/*
 * TelaCliente.java
 *
 * Created on __DATE__, __TIME__
 */

package telas.aluguel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import business.aluguel.Cliente;
import business.sistema.ExceptionSistema;
import business.sistema.IObjetoSistema;
import telas.comum.TelaMDI;

/**
 *
 * @author  __USER__
 */
public class TelaCliente extends TelaMDI
{
	private static final long serialVersionUID = 6234516160925882091L;

	/** Creates new form TelaCliente */
	public TelaCliente() throws Exception
	{
		initComponents();

		eventoBuscar();
		eventoNovo();
		eventoEditar();
		eventoExcluir();
	}

	private void eventoExcluir()
	{
		barraFerramenta1.getBtExcluir().addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					exluirCliente();
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private void exluirCliente() throws Exception
	{
		Cliente cliente = (Cliente) tblClientes.getObjetoCorrente();

		cliente.excluir();

		preencheTabelaClientes();
	}

	private void eventoEditar()
	{
		barraFerramenta1.getBtEditar().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					abrirTela(false);
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private void eventoNovo()
	{
		barraFerramenta1.getBtNovo().addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					abrirTela(true);
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private void abrirTela(boolean novo) throws Exception
	{
		Cliente cliente;

		if (novo)
		{
			cliente = new Cliente();
		} else
		{
			cliente = (Cliente) tblClientes.getObjetoCorrente();
		}

		new TelaClienteCadastro(cliente, novo);

		preencheTabelaClientes();
	}

	private void eventoBuscar()
	{
		barraFerramenta1.getBtBusca().addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					preencheTabelaClientes();
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private void preencheTabelaClientes() throws Exception
	{
		List<IObjetoSistema> clientes = new Cliente()
				.obterTodosPorFiltro(filtro());

		tblClientes.setDados(clientes);
		tblClientes.setColunas(colunasTabelaClientes());
		tblClientes.bind();
	}

	private String filtro() throws Exception
	{
		String criterio = "1 = 1";

		if (!jftCpf.getText().equals("   .   .   -  "))
		{
			criterio += " AND this_.cpf LIKE '%" + jftCpf.getText() + "%'";
		}

		if (!ctNome.getText().equals(""))
		{
			criterio += " AND UPPER(this_.nome) LIKE '%UPPER("
					+ ctNome.getText() + ")%'";
		}

		return criterio;
	}

	private ArrayList<String[]> colunasTabelaClientes()
	{
		ArrayList<String[]> dscColunas = new ArrayList<String[]>();
		dscColunas.add(new String[]
		{ "CPF", "getCpf", "30" });
		dscColunas.add(new String[]
		{ "Cliente", "getNome", "400" });

		return dscColunas;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents()
	{

		pnlPrincipal = new telas.componentes.Painel();
		barraFerramenta1 = new telas.componentes.BarraFerramenta();
		pnlCadastro = new telas.componentes.Painel();
		pnlFiltro = new telas.componentes.Painel();
		rotulo1 = new telas.componentes.Rotulo();
		rotulo2 = new telas.componentes.Rotulo();
		jftCpf = new javax.swing.JFormattedTextField();
		ctNome = new telas.componentes.CampoTexto();
		jScrollPane1 = new javax.swing.JScrollPane();
		tblClientes = new telas.componentes.Tabela();

		setTitle("Clientes");

		pnlPrincipal.setLayout(new java.awt.BorderLayout());

		barraFerramenta1.setRollover(true);
		pnlPrincipal.add(barraFerramenta1, java.awt.BorderLayout.PAGE_START);

		pnlCadastro.setBorder(javax.swing.BorderFactory
				.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
		pnlCadastro.setLayout(new java.awt.BorderLayout());

		pnlFiltro.setBorder(javax.swing.BorderFactory
				.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

		rotulo1.setText("CPF:");

		rotulo2.setText("Nome:");

		try
		{
			jftCpf
					.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
							new javax.swing.text.MaskFormatter("###.###.###-##")));
		} catch (java.text.ParseException ex)
		{
			ex.printStackTrace();
		}

		ctNome.setMaxLength(100);

		javax.swing.GroupLayout pnlFiltroLayout = new javax.swing.GroupLayout(
				pnlFiltro);
		pnlFiltro.setLayout(pnlFiltroLayout);
		pnlFiltroLayout
				.setHorizontalGroup(pnlFiltroLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								pnlFiltroLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pnlFiltroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																rotulo1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																rotulo2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(7, 7, 7)
										.addGroup(
												pnlFiltroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jftCpf,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																160,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																ctNome,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																611,
																Short.MAX_VALUE))
										.addContainerGap()));
		pnlFiltroLayout
				.setVerticalGroup(pnlFiltroLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								pnlFiltroLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pnlFiltroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																rotulo1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jftCpf,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(18, 18, 18)
										.addGroup(
												pnlFiltroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																rotulo2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																ctNome,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		pnlCadastro.add(pnlFiltro, java.awt.BorderLayout.PAGE_START);

		tblClientes.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][]
				{

				}, new String[]
				{

				}));
		jScrollPane1.setViewportView(tblClientes);

		pnlCadastro.add(jScrollPane1, java.awt.BorderLayout.CENTER);

		pnlPrincipal.add(pnlCadastro, java.awt.BorderLayout.CENTER);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				pnlPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 684,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				pnlPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 376,
				Short.MAX_VALUE));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private telas.componentes.BarraFerramenta barraFerramenta1;
	private telas.componentes.CampoTexto ctNome;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JFormattedTextField jftCpf;
	private telas.componentes.Painel pnlCadastro;
	private telas.componentes.Painel pnlFiltro;
	private telas.componentes.Painel pnlPrincipal;
	private telas.componentes.Rotulo rotulo1;
	private telas.componentes.Rotulo rotulo2;
	private telas.componentes.Tabela tblClientes;
	// End of variables declaration//GEN-END:variables

}